<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="servlets.message.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>
<%@ page import="hibernate.method.UserMethods"%>

<div class="page-header">
	<h2 class="page-title">Nowa wiadomość</h2>
</div>

<br />

<div id="content">
<%
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						UserMethods u_m = new UserMethods();
						
						Integer sender_id = (Integer) session.getAttribute("user_id");
 
						List<User> ulist = u_m.list(sessionFactory, s);
						s.getTransaction().commit();
						
					%> 
					<form action="../MessageSend" method="POST" name='formularz'>
					<input type="hidden" id="sender_id" name="sender_id" value="<%=sender_id %>"/>
		<table>
			<tr>
				<td>Wyślij wiadomość do użytkownika:</td>
				<td>
				<select id="receiver" name="receiver" style="width: 80%">
						<option value="0" selected>Wybierz użytkownika</option>
						<%		if (ulist.size() > 0) {
							
							for (User u1 : ulist) {
								if (u1==u_m.id_to_user(sessionFactory, s, sender_id)) 
								{
									int toRemove=ulist.indexOf(u1);
									//System.out.println(toRemove);
									
								//	ulist.remove(toRemove);
								
								if(ulist.size()==1) out.println ("Nie ma uzytkownikow do ktorych moglbys napisac");
								}
								else {
								String username = u1.getUsername();
								String usertype="";
								if ("worker".equals(u1.getType())) usertype="[obsługa]";
								if ("admin".equals(u1.getType())) usertype="[administracja]";
								int id = u1.getUser_id();%>
						<option value="<%=id%>"><%out.print(username+" "+usertype);%></option>
						<%}
							}
							
						}
						if (ulist.size() == 0) {
					out.println("Nie ma zadnych uzytkownikow w bazie");
						} %>
						</select>
				</td>
			</tr>
			<tr>
				<td>Temat wiadomości:</td>
				<td><input type="text" id="subject" name="subject" style="width: 80%"></input></td>
			</tr>
			<tr>
				<td>Treśc wiadomości:</td>
				<td><textarea id="content" name="content" cols="50" rows="10" style="width: 80%"> </textarea>
				</td>
			</tr>
			<tr>
				<td><input type="submit" class="mainBtn" value="Wyślij wiadomość" /></td>
			</tr>
		</table>
	</form>
						
</div>
